var agentinfo_api = {
    update: appConfig.server_url + "bus/extend/MA/update_MA/"
};
require(["coAjax"], function(coAjax) {
    // 修改头像
    App.set("$Event.agent.agentinfo.update_img", function() {
        var file = this.files[0];
        if (file) {
            var _upload_dtd = new $.Deferred();
            App.set("$Cache.agent.agentinfo.is_uploading", true);
            $.when(upload_image(file, _upload_dtd)).then(function(url) {
                App.set("agent_info.agent_img", url);
                App.set("$Cache.agent.agentinfo.is_uploading", false);
                coAjax.put(agentinfo_api.update + App.get("agentinfo._id"), {
                    agent_img: url
                }, function(result) {
                    console.log(result);
                }, function(errCode, errResponse, errMsg) {
                    console.log(errMsg);
                });
            });
        };
    });

    // 修改密码
    App.set("$Event.agent.agentinfo.update_password", function() {
        var update_password_data = App.get("$Cache.agent.agentinfo.update_password");
        if (!update_password_data) {
            alert("warn", "请先输入修改信息");
            return;
        };
        if (!update_password_data.old_password || !update_password_data.new_password) {
            alert("warn", "请先输入旧密码或新密码");
            return;
        };
        if (update_password_data.new_password.length < 6) {
            alert("warn", "密码长度不能小于6位");
            return;
        };
        if (update_password_data.new_password != update_password_data.confirm_new_password) {
            alert("warn", "两次输入新密码不一致");
            return;
        };
        coAjax.put(appConfig.user.change_password, update_password_data, function(result) {
            alert("success", "密码修改成功");
            App.set("$Cache.agent.agentinfo.update_password", "");
        }, function(errCode, errResponse, errMsg) {
            alert("warn", errMsg);
            console.log(errMsg);
        });
    });
});
